'''
Created on Apr 1, 2012

@author: e911miri
'''
import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='[]', db='orchard')

def result(reg_num=0, pin='0000', courses={}):
    cur = conn.cursor()
    user = None
    result = None
    #***Really light validation for the registration number**
    if len(reg_num) < 11 and len(pin) == 4:
        cur.execute("SELECT reg_num FROM candidates WHERE reg_num= %s AND pin= %s limit 1", (reg_num, pin))
        if cur.rowcount: #hurray, user was validated
            cur.execute("SELECT * from results WHERE reg_num= %s limit 1", reg_num)
            if cur.rowcount: result = cur.fetchone()
            #hurray, he has a result
            courses = result[1:-1]           
            return {'reg_num': result[0], 'user': user, 'result': [(courses[x], courses[x + 1]) for x in range(0, 8, 2)], 'score': result[len(result)-1]}
    #just in cse something goes wrong, he still has a number
    return {'reg_num': reg_num, 'error': 'result not found'}
            
    
